package com.niit.users.action;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.niit.common.pojo.ApiResponse;
import com.niit.common.pojo.ApiResponseEnum;
import com.niit.common.util.ConstatFinalUtil;
import com.niit.common.util.PageInfoUtil;
import com.niit.users.pojo.TbPassenger;
import com.niit.users.service.IUsersDbService;
import com.niit.users.service.impl.UsersDbServiceImpl;

/**
 * 用户相关的Action
 */
public class UsersAction {
    /**
     * Passenger相关的Service
     */
    private IUsersDbService usersDbService = new UsersDbServiceImpl();

    /**
     * 添加乘客
     * 
     * @param pojo
     * @return
     */
    public ApiResponse<Object> saveOnePassengerService(TbPassenger pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        if (Objects.isNull(pojo)) {
            apiResponse.setCode(ApiResponseEnum.INFO_EMPTY.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        if (Objects.nonNull(pojo) && "".equalsIgnoreCase(pojo.getPassengerIdCardNo())) {
            apiResponse.setCode(ApiResponseEnum.INFO_EMPTY.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        apiResponse = this.usersDbService.saveOnePassengerService(pojo);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 更新乘客
     * 
     * @param pojo
     * @return
     */
    public ApiResponse<Object> updateOnePassengerService(TbPassenger pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        apiResponse = this.usersDbService.updateOnePassengerService(pojo);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 删除乘客
     * 
     * @param condMap
     * @return
     */
    public ApiResponse<Object> deleteOnePassengerService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = this.usersDbService.deleteOnePassengerService(condMap);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 查询乘客列表
     * 
     * @param pageInfoUtil 为空表示不分页
     * @param condMap
     * @return
     */
    public ApiResponse<TbPassenger> findCondListPassengerService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<TbPassenger> apiResponse = this.usersDbService.findCondListPassengerService(pageInfoUtil, condMap);
        TbPassenger pojoUtil = new TbPassenger();
        pojoUtil.printTitle();
        List<TbPassenger> dataList = apiResponse.getDataListJava();
        /* 打印标题 */
        for (Iterator iterator = dataList.iterator(); iterator.hasNext();) {
            TbPassenger dataTemp = (TbPassenger) iterator.next();
            dataTemp.printInfo();
        }
        return apiResponse;
    }

    /**
     * 查询乘客单条记录
     * 
     * @param condMap
     * @return
     */
    public ApiResponse<TbPassenger> findOnePassengerService(Map<String, Object> condMap) {
        ApiResponse<TbPassenger> apiResponse = this.usersDbService.findOnePassengerService(condMap);
        TbPassenger dataTemp = apiResponse.getDataOneJava();
        if (Objects.nonNull(dataTemp)) {
            TbPassenger pojoUtil = new TbPassenger();
            pojoUtil.printTitle();
            dataTemp.printInfo();
        }
        return apiResponse;
    }
}
